let config = require('../../../utils/config');
let util = require('../../../utils/util');
let API_URL = config.API_URL;
let app = getApp();
let loading, page;
let getData = that => {
  if (loading) {
    return
  }

  loading = true;
  util.PRL(API_URL.recommends_my, { page: page })
    .then(res => {
      console.log(res)
      let data = res.data.data.recommends;
      let list;
      if (page == 1) {
        list = data;
      } else {
        list = that.data.list.concat(data);
      }
      if (res.data.data.hasMore == 1 || data.length != 0) {
        page++
      }
      that.setData({
        list: list
      })
      loading = false;
    }).catch(res => {
      loading = false;
      console.log(res);
    })
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    page = 1;
    getData(this);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log(app)
    if (app.globalData.refresh) {
      app.globalData.refresh = false;
      page = 1;
      getData(this);
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    page = 1;
    getData(this);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    getData(this);
  },
  previewImage: function (e) {
    let url = e.currentTarget.dataset.url;
    let index = e.currentTarget.dataset.index;
    let urls = this.data.list[index].images;
    my.previewImage({
      current: index,
      urls: urls
    })
  },
  edit: function (e) {
    let index = e.currentTarget.dataset.index;
    let recommend = this.data.list[index];
    console.log(this.data.list)
    console.log(recommend)
    if (recommend.status == 1) {
      return
    }
    app.globalData.recommends = recommend;
    console.log(app.globalData.recommends)
    my.navigateTo({
      url: '/pages/demand/recommend_news/recommend_news?recommendId=' + recommend.recommendId,
    })
  },
  del: function (e) {
    let that = this;
    let index = e.currentTarget.dataset.index;
    let list = that.data.list;
    let recommend = list[index];
    my.confirm({
      title: '温馨提示',
      content: '确定要删除吗',
      confirmButtonText: '删除',
      cancelButtonText: '取消',
      success: (result) => {
        util.PRL(API_URL.recommends_del, { recommend_id: recommend.recommendId })
          .then(res => {
            console.log(res);
            my.showToast({
              content: '删除成功', // 文字内容
              duration: 800,
              success: (res) => {
                list.splice(index, 1);
                that.setData({
                  list: list
                })
              },
              fail: (res) => {
                my.showToast({
                  content: "删除失败"
                });
              }
            });
          })
      },
    });
  }
})